package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.tengbo.PushDatasUtil;
import com.xmtlxx.common.tengbo.vo.SuperviseDatasVo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.Supervise;
import com.xmtlxx.wms.domain.bo.SuperviseBo;
import com.xmtlxx.wms.domain.vo.SuperviseVo;
import com.xmtlxx.wms.mapper.SuperviseMapper;
import com.xmtlxx.wms.service.ISuperviseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 监督反馈Service业务层处理
 *
 * @author xmtlxx
 * @date 2023-04-04
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SuperviseServiceImpl implements ISuperviseService {

    private final SuperviseMapper baseMapper;

    /**
     * 查询监督反馈
     *
     * @param id 监督反馈主键
     * @return 监督反馈
     */
    @Override
    public SuperviseVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectSuperviseVoByID(id);
    }

    /**
     * 查询监督反馈列表
     *
     * @param bo 监督反馈
     * @return 监督反馈
     */
    @Override
    public TableDataInfo<SuperviseVo> queryPageList(SuperviseBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<Supervise> lqw = buildQueryWrapper(bo);
       // Page<SuperviseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<SuperviseVo> result = baseMapper.selectPageSuperviseList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询监督反馈列表
     *
     * @param bo 监督反馈
     * @return 监督反馈
     */
    @Override
    public List<SuperviseVo> queryList(SuperviseBo bo) {
       // LambdaQueryWrapper<Supervise> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectSuperviseList(bo);
    }

    private LambdaQueryWrapper<Supervise> buildQueryWrapper(SuperviseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Supervise> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增监督反馈
     *
     * @param bo 监督反馈
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SuperviseBo bo) {
        Supervise add = BeanUtil.toBean(bo, Supervise.class);
        validEntityBeforeSave(add);

        //填充UUID
        if(StringUtils.isEmpty(add.getId())){
            add.setId(IdUtil.simpleUUID());
        }
        add.setDelFlag("0");
        add.setStatus("0");

        if(!LoginHelper.checkLoginUser()){
            add.setCreateBy("admin");
            add.setCreateTime(new Date());
            add.setUpdateBy("admin");
            add.setUpdateTime(new Date());
        }

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改监督反馈
     *
     * @param bo 监督反馈
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SuperviseBo bo) {
        Supervise update = BeanUtil.toBean(bo, Supervise.class);
        validEntityBeforeSave(update);

        if(update.getStatus().equals("0")){//待回复
            update.setStatus("1");
            update.setBacker(LoginHelper.getNickname());
            update.setBackTime(new Date());
        }
        boolean flag = baseMapper.updateById(update) > 0;
        if(flag){
            pushDatas(update);
        }

        return flag;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Supervise entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除监督反馈
     *
     * @param ids 需要删除的监督反馈主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    //推送反馈
    public void pushDatas(Supervise supervise){
        SuperviseDatasVo data = new SuperviseDatasVo();
        BeanUtil.copyProperties(supervise,data);
        PushDatasUtil.pushSuperviseDatas(data);
    }
}
